package com.lh.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lh.entity.table.*;
import com.lh.mapper.DeadlineNodeApprovalMapper;
import com.lh.mapper.RectificationInfoApprovalMapper;
import com.lh.mapper.RectificationInfoMapper;
import com.lh.service.DeadlineNodeApprovalService;
import com.lh.utils.Answer;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class DeadlineNodeApprovalServiceImpl implements DeadlineNodeApprovalService {

    @Resource
    private DeadlineNodeApprovalMapper deadlineNodeApprovalMapper;

    @Resource
    private RectificationInfoApprovalMapper rectificationInfoApprovalMapper;

    @Resource
    private RectificationInfoMapper rectificationInfoMapper;

    @Override
    public Answer<Object> commitApproval(JSONObject reqJson) {
        try {
            int id = 0;
            JSONArray data = reqJson.getJSONArray("data");
            Integer rectificationId = reqJson.getInteger("rectificationId");
            deadlineNodeApprovalMapper.delete(new LambdaQueryWrapper<DeadlineNodeApproval>().eq(DeadlineNodeApproval::getRectificationId, rectificationId));
            for (int i = 0; i < data.size(); i++) {
                JSONObject jsonObject = data.getJSONObject(i);
                DeadlineNodeApproval deadlineNodeApproval = JSONObject.parseObject(jsonObject.toJSONString(), DeadlineNodeApproval.class);
                if (rectificationId == null) {
                    return Answer.error("任务id不能为空");
                }
                if (i == 0) {
                    RectificationInfoApproval rectificationInfoApproval = rectificationInfoApprovalMapper.selectOne(new LambdaQueryWrapper<RectificationInfoApproval>()
                            .eq(RectificationInfoApproval::getRectificationId,rectificationId).eq(RectificationInfoApproval::getApprovalState,0)
                            .ne(RectificationInfoApproval::getState,"已关闭").ne(RectificationInfoApproval::getState,"已销号").
                            ne(RectificationInfoApproval::getState,"已退出"));
                    RectificationInfo rectificationInfo = rectificationInfoMapper.selectById(rectificationId);
                    if (rectificationInfoApproval == null && rectificationInfo ==null) {
                        continue;
                    }
                }
                deadlineNodeApproval.setCreateTime(new Date());
                deadlineNodeApproval.setSort(i);
                deadlineNodeApproval.setState("1");
                deadlineNodeApproval.setRectificationId(rectificationId);
                deadlineNodeApproval.setTypeName("整改");
                deadlineNodeApprovalMapper.insert(deadlineNodeApproval);
            }
            return Answer.ok("添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Answer.error("添加失败");
        }
    }

    @Override
    public Answer<Object> updateDeadlineNodeApproval(DeadlineNodeApproval deadlineNodeApproval) {
        deadlineNodeApprovalMapper.updateById(deadlineNodeApproval);
        return Answer.ok("修改成功");
    }

    @Override
    public Answer<Object> deleteDeadlineNodeApproval(JSONObject reqJson) {
        Integer id = reqJson.getInteger("id");
        deadlineNodeApprovalMapper.deleteById(id);
        return Answer.ok("修改成功");
    }
}
